namespace Common
{
    using System;
    using System.Collections.Generic;
    using System.Text;

    public class StringPlus
    {
        public static string DelLastChar(string str, string strchar)
        {
            return str.Substring(0, str.LastIndexOf(strchar));
        }

        public static string DelLastComma(string str)
        {
            return str.Substring(0, str.LastIndexOf(","));
        }

        public static string GetArrayStr(List<string> list, string speater)
        {
            StringBuilder builder = new StringBuilder();
            for (int i = 0; i < list.Count; i++)
            {
                if (i == (list.Count - 1))
                {
                    builder.Append(list[i]);
                }
                else
                {
                    builder.Append(list[i]);
                    builder.Append(speater);
                }
            }
            return builder.ToString();
        }

        public static string[] GetStrArray(string str)
        {
            return str.Split(new char[0x2c]);
        }
        public static string GetMinString(string str, int length)
        {
            if (string.IsNullOrEmpty(str))
            {
                return "";
            }
            string subfix = "...";

            //length = length - subfix.Length;
            StringBuilder s = new StringBuilder();
            int tempLength = 0;
            foreach (char var in str)
            {

                if (var > 255)
                {
                    tempLength += 2;
                }
                else
                {
                    tempLength += 1;
                }
                if (tempLength > length)
                {
                   RemoveString(s, subfix);
                   
                    break;
                }
                s.Append(var);
            }

            return s.ToString();

        }

        private static void RemoveString(StringBuilder s, string subfix)
        {
            int length = subfix.Length;
            int tempLength = 0;
            for (int i = 1; i <= length; i++)
            {
                char var=s[s.Length - i];
                if (var>255)
                {
                    tempLength += 2;
                }
                else
                {
                    tempLength += 1;
                }
                s.Remove(s.Length - 1, 1);
                if (tempLength>length)
                {
                    s.Append(subfix);
                    break;
                }
               
			
            }
            
        }
    }
}

